<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta content="Bmob,BmobCloud,bomb,BaaS,mBaaS,PaaS,Serverless,FaaS,Function as a Service,Backend as a Service,serverless computing,cloud function,后端云,bmob后端云,小程序云,小程序后端,云数据库,云存储,文件存储,云函数,云端代码,定时任务,游戏后端,游戏云,用户系统,无服务器函数,移动开发,app开发,小程序开发,云端一体化,互联网中间件" name="keywords"/>
            <meta content="国内首家专注于移动应用Serverless云服务的平台,Bmob后端云让移动开发更简单,全方位一体化的后端服务平台" name="description"/>
            
            
            
            <link rel="shortcut icon" href="../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <!--
            <title>快速入门 - Bmob文档中心</title>
            -->
            <title>云函数 &middot; Web – Bmob后端云 </title>        
            
            <link href="../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../css/highlight.css">
            <link href="../../css/agate.css" rel="stylesheet">
            <link href="../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../..">
                <!--Bmob文档中心-->
                <img src="../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../..">文档首页</a>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../data/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/ios/">iOS</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/csharp/">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/restful/">REST API</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/kotlin/">Kotlin</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/python/">Python</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="../android/">云函数</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../java/">Java</a>
                                </li>
							
                            
                                <li >
                                    <a href="../python/">Python</a>
                                </li>
							
                            
                                <li >
                                    <a href="../csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../restful/">REST API</a>
                                </li>
							
                            
								<li class="active">
									<a href="./">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../game/unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../game/cloud_function/develop_doc/">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../game/classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">即时通讯</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">即时通讯 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../push/android/">推送服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../push/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../push/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
				
				
				
				
					<div class="code-title">Web</div>
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
		
			
				
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
							
								
									<li class="active">
										<a class="itm-l1" href="./">快速入门</a>
										
										<ul class="nav">
											
											<li class="active "><a href="#bmob">注册Bmob帐号</a>
												
											</li>
												
											<li class=""><a href="#_1">网站后台创建应用</a>
												
											</li>
												
											<li class=""><a href="#sdk">获取应用密钥和下载SDK</a>
												
											</li>
												
											<li class=""><a href="#_2">创建云函数</a>
												
											</li>
												
											<li class=""><a href="#bmobsdk">安装和初始化BmobSDK</a>
												
											</li>
												
											<li class=""><a href="#_3">调用云函数</a>
												
												<ul class="nav nav-l2">
													<li><a class="itm-l2" href="#android">Android调用云函数</a></li>
												</ul>
												
												<ul class="nav nav-l2">
													<li><a class="itm-l2" href="#ios">iOS调用云函数</a></li>
												</ul>
												
											</li>
												
											<li class=""><a href="#_4">源码下载</a>
												
											</li>
												
										</ul>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="develop_doc/">开发文档</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="timing_tasks/">定时任务</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="weixin/">微信云函数</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="norm/">编码规范</a>
										
									</li>
								
							
						
					
				
			
		
			
		
			
		
			
		
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<h2 id="bmob">注册Bmob帐号<a class="headerlink" href="#bmob" title="Permanent link">&para;</a></h2>
<p>在网址栏输入www.bmob.cn或者在百度输入Bmob进行搜索，打开Bmob官网后，点击右上角的“注册”，在跳转页面填入你的姓名、邮箱、设置密码，确认后到你的邮箱激活Bmob账户，你就可以用Bmob轻松开发应用了。</p>
<p><img alt="" src="./image/rumen_zhuce.png" /></p>
<h2 id="_1">网站后台创建应用<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h2>
<p>登录账号进入bmob后台后，点击后台界面左上角“创建应用”，在弹出框输入你应用的名称，然后确认，你就拥有了一个等待开发的应用。</p>
<p><img alt="" src="./image/rumen_chuangjian.png" /></p>
<h2 id="sdk">获取应用密钥和下载SDK<a class="headerlink" href="#sdk" title="Permanent link">&para;</a></h2>
<p>选择你要开发的应用，进入该应用</p>
<p><img alt="" src="./image/rumen_miyue_1.png" /></p>
<p>在跳转页面，进入设置/应用密钥，点击复制，即可得到Application ID</p>
<p><img alt="" src="./image/rumen_miyue_2.png" /></p>
<p>获取Application ID后，下载SDK，开发者可以根据自己的需求选择相应的iOS SDK 或Android SDK，点击下载即可。</p>
<p><img alt="" src="./image/rumen_miyue_3.png" /></p>
<h2 id="_2">创建云函数<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h2>
<p>在Bmob后台中，选择你刚刚创建好的应用，然后依次点击“云函数-&gt;添加方法”，在弹出窗口中输入云函数的方法名，该方法名将会在SDK调用时使用到。如下图所示：</p>
<p><img alt="" src="./image/cloudcodeadd1.png" /></p>
<p><img alt="" src="./image/cloudcodeadd2.png" /></p>
<p>接着，你就可以在云函数的编辑器中撰写云函数了。如果你熟悉js脚本语言的话，撰写云函数非常简单，你只需要在onRequest方法中补充你的业务逻辑代码就可以了。onRequest方法包含3个参数，分别是request（请求对象，可以从中获取SDK上传的参数）, response（回应对象，可以将云函数的执行结果返回到SDK中）, modules（可调用的模块，包含数据库对象、HTTP对象等）。为方便演示，这里简单实现一个功能：接收客户端上传上来的name参数，根据name的值返回不同的结果。代码如下：</p>
<pre><code class="java">function onRequest(request, response, modules) {
  //获取SDK客户端上传的name参数
  var name = request.body.name;
    if(name == 'bmob')
      response.end('欢迎使用Bmob');
    else
      response.end('输入错误，请重新输入');
}                         
</code></pre>

<h2 id="bmobsdk">安装和初始化BmobSDK<a class="headerlink" href="#bmobsdk" title="Permanent link">&para;</a></h2>
<p>云函数的调用执行可以是在云端（通过“定时任务”模块去设置执行，实现定时业务逻辑计算的功能，如排行榜，这个操作非常简单，这里不再一一介绍），但更多的开发者是通过SDK调用的方式来执行云函数。这有点像存储过程，大家可以将更多的业务逻辑放在云端，可以随意改变，不需要更新应用，也不需要上传太多的数据参数。</p>
<p>安装和初始化SDK的过程大家根据各自的平台（<a href="http://docs.bmob.cn/data/Android/a_faststart/doc/index.html" title="Android快速入门文档">Android</a> / <a href="http://docs.bmob.cn/data/iOS/a_faststart/doc/index.html" title="iOS快速入门文档">iOS</a>等）具体参考快速入门文档即可。</p>
<h2 id="_3">调用云函数<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h2>
<p>将如下的代码根据各自平台，插入到触发执行的方法里面。</p>
<h3 id="android">Android调用云函数<a class="headerlink" href="#android" title="Permanent link">&para;</a></h3>
<pre><code class="java">//test对应你刚刚创建的云函数名称
String cloudCodeName = &quot;test&quot;;
JSONObject params = new JSONObject();
//name是上传到云端的参数名称，值是bmob，云函数可以通过调用request.body.name获取这个值 
params.put(&quot;name&quot;, &quot;bmob&quot;);
//创建云函数对象
AsyncCustomEndpoints cloudCode = new AsyncCustomEndpoints();
//异步调用云函数
cloudCode.callEndpoint(MainActivity.this, cloudCodeName, params, new CloudCodeListener() {

    //执行成功时调用，返回result对象
    @Override
    public void onSuccess(Object result) {
        Log.i(&quot;bmob&quot;, &quot;result = &quot;+result.toString());
    }

    //执行失败时调用
    @Override
    public void onFailure(String err) {
        Log.i(&quot;bmob&quot;, &quot;BmobException = &quot;+err);
    }
});
</code></pre>

<h3 id="ios">iOS调用云函数<a class="headerlink" href="#ios" title="Permanent link">&para;</a></h3>
<pre><code class="java">    //name是上传到云端的参数名称，值是bmob，云函数可以通过调用request.body.name获取这个值 
    NSDictionary  *dic = [NSDictionary  dictionaryWithObject:@&quot;bmob&quot; forKey:@&quot;name&quot;];
    //test对应你刚刚创建的云函数名称
    [BmobCloud callFunctionInBackground:@&quot;test&quot; withParameters:dic block:^(id object, NSError *error) {

    if (!error) {
        //执行成功时调用
        NSLog(@&quot;error %@&quot;,[object description]);
    }else{
       //执行失败时调用
        NSLog(@&quot;error %@&quot;,[error description]);
    }

    }] ;
</code></pre>

<h2 id="_4">源码下载<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h2>
<p><a href="https://github.com/bmob/bmob-ios-demo/blob/master/CloudFunction.zip">iOS源码下载</a></p>
<p><a href="http://www.bmob.cn/static/Bmob_Sample_android_cloud.zip" title="Android源码下载">Android源码下载</a></p>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../..';</script>
            <script src="../../js/jquery-1.10.2.min.js"></script>
            <script src="../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../js/highlight.pack.js"></script>
            <script src="../../js/main.js"></script>
            <script src="../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>